<template>
  <div>

    <el-button @click="addChapter">
      添加章节
    </el-button>
    <!-- 章节列表 -->
    <ul class="chapterList">
      <li
        v-for="chapter in chapterList"
        :key="chapter.id">
        <p>
          {{ chapter.title }}
          <span class="acts">
            <el-button type="text" @click="addVideo(chapter)">添加课时</el-button>
            <el-button type="text" @click="editChapter(chapter)">编辑</el-button>
            <el-button type="text">删除</el-button>
          </span>
        </p>
        <!-- 视频 -->
        <ul class="chapterList videoList">
          <li
            v-for="video in chapter.videos"
            :key="video.id">
            <p>
              {{ video.title }}
              <el-tag v-if="!video.videoSourceId" size="mini" type="danger">
                {{ '尚未上传视频' }}
              </el-tag>
              <el-tag v-else size="mini" type="success">
                {{ '已上传视频' }}
              </el-tag>
              <span class="acts">
                <el-tag v-if="video.free" size="mini" type="success">{{ '免费观看' }}</el-tag>
                <el-button type="text" @click="editVideo(video.id)">编辑</el-button>
                <el-button type="text">删除</el-button>
              </span>
            </p>
          </li>
        </ul>
      </li>
    </ul>
    <div style="text-align:center">
      <el-button type="success" @click="prev">上一步</el-button>
      <el-button type="success" @click="next">下一步</el-button>
    </div>

    <chapter-form ref="ChapterForm"/>
    <video-form ref="videoForm"/>
  </div>
</template>
<script>
import ChapterForm from '@/views/course/components/step2/chapterForm.vue'
import chapterApi from '@/api/chapter'
import videoForm from '@/views/course/components/step2/videoForm.vue'
export default {
  components: {
    ChapterForm,
    videoForm
  },
  data() {
    return {
      chapterList: []

    }
  },
  created() {
    this.getChapterAndVideo()
  },
  methods: {
    prev() {
      this.$parent.active = 0
    },
    next() {
      this.$parent.active = 2
    },
    addChapter() {
      this.$refs.ChapterForm.dialogFormVisible = true
    },
    // 根据课程id查询并显示章节及课时信息
    getChapterAndVideo() {
      chapterApi.getChapters(this.$parent.courseId).then(r => {
        this.chapterList = r.data.items
      })
    },
    editChapter(chapter) {
      this.$refs.ChapterForm.open(chapter.id)
    },
    addVideo(chapter) {
      this.$refs.videoForm.addVideo(chapter.id)
    },
    editVideo(videoId) {
      this.$refs.videoForm.open(videoId)
    }
  }
}
</script>
<style>
.chapterList{
    position: relative;
    list-style: none;
    margin: 0;
    padding: 0;
}
.chapterList li{
  position: relative;
}
.chapterList p{
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #DDD;
}
.chapterList .acts {
    float: right;
    font-size: 14px;
}

.videoList{
  padding-left: 50px;
}
.videoList p{
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dashed #DDD;
}
</style>
